var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "maishu-chitu", "../common", "maishu-services-sdk", "mobile/error-handle"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const chitu = require("maishu-chitu");
    const common_1 = require("../common");
    const maishu_services_sdk_1 = require("maishu-services-sdk");
    const error_handle_1 = require("mobile/error-handle");
    let protocol = location.protocol;
    class Service extends maishu_services_sdk_1.Service {
        // static get token() {
        //     return getCookie("token");
        // }
        // static set token(value: string) {
        //     setCookie("token", value, 365);
        // }
        static createInstance(type) {
            let service = new type();
            //TODO: 处理错误
            service.error.add((sender, error) => {
                // alert({ title: "错误", message: error.message });
                error_handle_1.errorHandle(error);
            });
            return service;
        }
        ajax(url, options) {
            const _super = Object.create(null, {
                ajax: { get: () => super.ajax }
            });
            return __awaiter(this, void 0, void 0, function* () {
                if (!url.startsWith("http")) {
                    let host = location.host;
                    let pathname = location.pathname;
                    if (!pathname.endsWith("/")) {
                        // pathname = pathname + "/";
                        let arr = pathname.split("/");
                        arr[arr.length - 1] = "";
                        pathname = arr.join("/");
                    }
                    if (url.startsWith("/")) {
                        url = `${protocol}//${host}${url}`;
                    }
                    else {
                        url = `${protocol}//${host}${pathname}${url}`;
                    }
                }
                console.log(url);
                options = options || {};
                options.headers = options.headers || {};
                if (this.applicationId)
                    options.headers['application-id'] = this.applicationId;
                // if (urlParams.token) {
                //     options.headers['token'] = urlParams.token;
                // }
                // else if (Service.token.value != null && Service.token.value != null) {
                //     options.headers['token'] = Service.token.value;
                // }
                return _super.ajax.call(this, url, options);
            });
        }
        static get storeName() {
            return "";
        }
        get protocol() {
            return location.protocol;
        }
        get applicationId() {
            return common_1.urlParams.appKey;
        }
        static get appToken() {
            return common_1.urlParams.token;
        }
    }
    exports.Service = Service;
    Service.token = new chitu.ValueStore(getCookie("token"));
    function imageUrl(path, width, height) {
        let imageService = new maishu_services_sdk_1.ImageService();
        return imageService.imageSource(path, width, height);
    }
    exports.imageUrl = imageUrl;
    Service.token.add((value) => {
        setCookie("token", value, 365);
    });
    function getCookie(name) {
        if (typeof document == 'undefined')
            return null;
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ')
                c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0)
                return c.substring(nameEQ.length, c.length);
        }
        return null;
    }
    function setCookie(name, value, days) {
        value = value || "";
        // nodejs 没有 document
        if (typeof document == 'undefined')
            return;
        var expires = "";
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            expires = "; expires=" + date.toUTCString();
        }
        document.cookie = name + "=" + (value || "") + expires + "; path=/";
    }
});
